// pages/home/device/setting.js
var app = getApp();

var page_did = "";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:null,
    push_id:0,
    receiver:[],
    alarmconfig:[],
    deviceconfig: [],
    txt_name: "",
    txt_tags: "",
    txt_group: "",
    txt_address: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options);
    page_did = options.did;//name
    var name = options.name;
    this.setData({ devicename: name });
    // that.setData({ alarm: res.data.data });
    //console.log("page_did:" + page_did+" Name:"+name)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getdeviceconfig();
    this.getalarmconfig();
    this.getpush();
    this.getdevice();
  },
  listerner_name: function (e) {this.setData({txt_name: e.detail.value})},
  listerner_tags: function (e) { this.setData({ txt_tags: e.detail.value }) },
  listerner_group: function (e) { this.setData({ txt_group: e.detail.value }) },
  listerner_address: function (e) { this.setData({ txt_address: e.detail.value }) },
  getdeviceattribute: function (attr) {
    var rs = "";
    switch (attr) {
      case "NickName": rs = this.data.txt_name; break;
      // case "Agent": rs = "sht3x"; break;
      // case "SIM": rs = "acs01"; break;
      case "Tags": rs = this.data.txt_tags;; break;
      case "Group": rs = this.data.txt_group;; break;
      case "Address": rs = this.data.txt_address;; break;
    }
    return rs;
  },
  //修改设备名称
  ondeviceattribute: function (e) {
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    var attr = e.currentTarget.dataset.attribute;
    var v = this.getdeviceattribute(attr);
    console.log("attr:" + attr+" v:"+v);
    wx.request({
      url: app.globalData.Url + "Api/Device/SetDeviceAttribute", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
        attribute: attr,
        value: v,
      },
      header: {'content-type': 'application/json'},
      success: function (res) {
        var msg = "";
        if (res.data.result) {
          msg = "修改设备名称成功";
        }
        else {
          msg = res.data.data;
        }
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 1000
        })
        //console.log(res.data);
      }
    })
  },
  //获取报警人
  getpush:function(e){
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/GetMessageReceiver", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        if (res.data.result)
        {
          that.setData({ receiver: res.data.data });
        }
      }
    })
  },
  //删除接收人
  ondeletepush:function(e)
  {
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.data.push_id = id;
    //console.log("id:" + that.data.push_id);
    wx.showModal({
      title: '删除',
      content: '确定删除该报警接收人?',
      success: function (res) {
        if (res.confirm) {
          that.deletepush();
        } 
      }
    })
  },
  //删除
  deletepush:function(){
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/DeleteMessageReceiver", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        id: that.data.push_id,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        var msg = "";
        if (res.data.result) {
          msg = "报警接收人删除成功";
        }
        else {
          msg = res.data.data;
        }
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 1000
        })
        //console.log(res.data);
        that.getpush();
      }
    })
  },
  //获取设备报警配置
  getalarmconfig:function(){
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/GetAlarmConfig", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        if (res.data.result){
          that.setData({ alarmconfig: res.data.data });
        }
      }
    })
  },
  //获取设备配置
  getdeviceconfig:function()
  {
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/GetDeviceConfig", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        if (res.data.result) {
          that.setData({ deviceconfig: res.data.data });
        }
      }
    })
  },
  //gosetdeviceconfig
  gosetdeviceconfig: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var unit = e.currentTarget.dataset.unit;
    var formula = e.currentTarget.dataset.formula;
    var enable = e.currentTarget.dataset.enable?1:0;
    //console.log("did:" + e.currentTarget.dataset.enable + " page_did:" + page_did);
    wx.navigateTo({
      url: '/pages/home/device/deviceconfig?id=' + id + "&name=" + name + "&unit=" + unit + "&formula=" + formula + "&enable=" + enable,
    })
  },
  //gosetalarmconfig
  gosetalarmconfig: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var value = e.currentTarget.dataset.value;
    var value2 = e.currentTarget.dataset.value2;
    var type = e.currentTarget.dataset.type;
    var enable = e.currentTarget.dataset.enable?1:0;
    //console.log("value2:" + value2);
    wx.navigateTo({
      url: '/pages/home/device/alarmconfig?did=' + page_did + '&type=' + type + "&name=" + name + "&value=" + value + "&value2=" + value2 + "&enable=" + enable,
    })
  },
  gosetreceiver:function(e){
    wx.navigateTo({
      url: '/pages/home/device/receiver?did=' + page_did,
    })
  },
  //获取设备数据
  getdevice: function (e) {
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    //console.log("getdevice");
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;

    wx.request({
      url: app.globalData.Url + "Api/Device/GetDevice", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        that.setData({ info: res.data.data.Info });
        that.setData({ txt_name: res.data.data.Info.Name })
        that.setData({ txt_tags: res.data.data.Info.Tags})
        that.setData({ txt_group: res.data.data.Info.Group })
        that.setData({ txt_address: res.data.data.Info.Address })
      }
    })
  },
})